---
title: Adapters
description: Pluggable infrastructure for state, streams, events, and cron
---

Adapters let you swap infrastructure implementations without changing your code. Use defaults for single-instance deployments or distributed adapters for scaling.

## How It Works

Configure adapters in `motia.config.ts`. If you don't specify any, Motia uses defaults.

```typescript title="motia.config.ts"
import { config } from '@motiadev/core'
import { RedisStateAdapter } from '@motiadev/adapter-redis-state'

const redisUrl = `redis://${process.env.REDIS_HOST || 'localhost'}:6379`

export default config({
  adapters: {
    state: new RedisStateAdapter({ url: redisUrl })
  }
})
```

Your Step code stays the same:

```typescript
await state.set('orders', 'order-123', { id: 'order-123' })
```

---

## Adapter Types

| Type | What it handles | Default | Distributed |
|------|----------------|---------|-------------|
| **State** | Key-value storage | File | Redis |
| **Streams** | Real-time data | File | Redis |
| **Events** | Event queues | In-memory | RabbitMQ |
| **Cron** | Job locking | In-memory | Redis |

---

## Default Adapters

For single-instance deployments. No configuration needed.

```typescript title="motia.config.ts"
import { config } from '@motiadev/core'

export default config({
  // Uses defaults:
  // - FileStateAdapter for state
  // - FileStreamAdapter for streams
  // - InMemoryQueueEventAdapter for events
  // - InMemoryCronAdapter for cron
})
```

**Where data goes:**
- State: `.motia/motia.state.json`
- Streams: `.motia/streams/`
- Events: Process memory
- Cron: Process memory

---

## Distributed Adapters

For multi-instance deployments. Shares state and events across instances.

### Redis State

<Tabs items={['TypeScript', 'JavaScript']}>
<Tab value='TypeScript'>

```typescript title="motia.config.ts"
import { config } from '@motiadev/core'
import { RedisStateAdapter } from '@motiadev/adapter-redis-state'

const redisUrl = `redis://${process.env.REDIS_HOST || 'localhost'}:${process.env.REDIS_PORT || '6379'}`

export default config({
  adapters: {
    state: new RedisStateAdapter(
      { url: redisUrl },
      { keyPrefix: 'myapp:', ttl: 3600 }
    )
  }
})
```

</Tab>
<Tab value='JavaScript'>

```javascript title="motia.config.js"
const { config } = require('@motiadev/core')
const { RedisStateAdapter } = require('@motiadev/adapter-redis-state')

const redisUrl = `redis://${process.env.REDIS_HOST || 'localhost'}:${process.env.REDIS_PORT || '6379'}`

module.exports = config({
  adapters: {
    state: new RedisStateAdapter(
      { url: redisUrl },
      { keyPrefix: 'myapp:', ttl: 3600 }
    )
  }
})
```

</Tab>
</Tabs>

**Install:**

```bash
npm install @motiadev/adapter-redis-state
```

### Redis Streams

<Tabs items={['TypeScript', 'JavaScript']}>
<Tab value='TypeScript'>

```typescript title="motia.config.ts"
import { config } from '@motiadev/core'
import { RedisStreamAdapterManager } from '@motiadev/adapter-redis-streams'

const redisUrl = `redis://${process.env.REDIS_HOST || 'localhost'}:${process.env.REDIS_PORT || '6379'}`

export default config({
  adapters: {
    streams: new RedisStreamAdapterManager({ url: redisUrl })
  }
})
```

</Tab>
<Tab value='JavaScript'>

```javascript title="motia.config.js"
const { config } = require('@motiadev/core')
const { RedisStreamAdapterManager } = require('@motiadev/adapter-redis-streams')

const redisUrl = `redis://${process.env.REDIS_HOST || 'localhost'}:${process.env.REDIS_PORT || '6379'}`

module.exports = config({
  adapters: {
    streams: new RedisStreamAdapterManager({ url: redisUrl })
  }
})
```

</Tab>
</Tabs>

**Install:**

```bash
npm install @motiadev/adapter-redis-streams
```

### RabbitMQ Events

<Tabs items={['TypeScript', 'JavaScript']}>
<Tab value='TypeScript'>

```typescript title="motia.config.ts"
import { config } from '@motiadev/core'
import { RabbitMQEventAdapter } from '@motiadev/adapter-rabbitmq-events'

export default config({
  adapters: {
    events: new RabbitMQEventAdapter({
      url: process.env.RABBITMQ_URL || 'amqp://localhost',
      exchangeName: 'motia.events',
      exchangeType: 'topic'
    })
  }
})
```

</Tab>
<Tab value='JavaScript'>

```javascript title="motia.config.js"
const { config } = require('@motiadev/core')
const { RabbitMQEventAdapter } = require('@motiadev/adapter-rabbitmq-events')

module.exports = config({
  adapters: {
    events: new RabbitMQEventAdapter({
      url: process.env.RABBITMQ_URL || 'amqp://localhost',
      exchangeName: 'motia.events',
      exchangeType: 'topic'
    })
  }
})
```

</Tab>
</Tabs>

**Install:**

```bash
npm install @motiadev/adapter-rabbitmq-events
```

### Redis Cron

<Tabs items={['TypeScript', 'JavaScript']}>
<Tab value='TypeScript'>

```typescript title="motia.config.ts"
import { config } from '@motiadev/core'
import { RedisCronAdapter } from '@motiadev/adapter-redis-cron'

const redisUrl = `redis://${process.env.REDIS_HOST || 'localhost'}:${process.env.REDIS_PORT || '6379'}`

export default config({
  adapters: {
    cron: new RedisCronAdapter({ url: redisUrl })
  }
})
```

</Tab>
<Tab value='JavaScript'>

```javascript title="motia.config.js"
const { config } = require('@motiadev/core')
const { RedisCronAdapter } = require('@motiadev/adapter-redis-cron')

const redisUrl = `redis://${process.env.REDIS_HOST || 'localhost'}:${process.env.REDIS_PORT || '6379'}`

module.exports = config({
  adapters: {
    cron: new RedisCronAdapter({ url: redisUrl })
  }
})
```

</Tab>
</Tabs>

**Install:**

```bash
npm install @motiadev/adapter-redis-cron
```

---

## All Together

Using all distributed adapters:

```typescript title="motia.config.ts"
import { config } from '@motiadev/core'
import { RedisStateAdapter } from '@motiadev/adapter-redis-state'
import { RedisStreamAdapterManager } from '@motiadev/adapter-redis-streams'
import { RabbitMQEventAdapter } from '@motiadev/adapter-rabbitmq-events'
import { RedisCronAdapter } from '@motiadev/adapter-redis-cron'

const redisUrl = `redis://${process.env.REDIS_HOST || 'localhost'}:6379`

export default config({
  adapters: {
    state: new RedisStateAdapter(
      { url: redisUrl },
      { keyPrefix: 'myapp:state:' }
    ),
    streams: new RedisStreamAdapterManager({ url: redisUrl }),
    events: new RabbitMQEventAdapter({
      url: process.env.RABBITMQ_URL || 'amqp://localhost',
      exchangeName: 'motia.events',
      exchangeType: 'topic'
    }),
    cron: new RedisCronAdapter(
      { url: redisUrl },
      { keyPrefix: 'myapp:cron:' }
    )
  }
})
```

---

## When to Use What

**Default adapters:**
- Single Motia instance
- Development
- Testing
- Simple deployments

**Distributed adapters:**
- Multiple Motia instances
- Horizontal scaling
- Production deployments
- High availability

---

## Configuration Tips

### Redis URL Format

Build Redis URLs from environment variables:

```typescript
// Basic connection
const redisUrl = `redis://localhost:6379`

// With password
const redisUrl = `redis://:password@localhost:6379`

// With username and password
const redisUrl = `redis://user:password@localhost:6379`

// From environment variables
const redisUrl = `redis://${process.env.REDIS_HOST}:${process.env.REDIS_PORT}`
```

### Adapter Options

Redis adapters accept two parameters:

```typescript
new RedisStateAdapter(
  { url: redisUrl },              // Connection config
  { keyPrefix: 'app:', ttl: 3600 } // Adapter options
)
```

**Common options:**
- `keyPrefix` - Prefix for all keys (helps organize data)
- `ttl` - Time-to-live in seconds (auto-expire old data)
- `lockTTL` - Lock timeout for cron jobs

---

